package com.ming.reggie.mapper;

import com.ming.reggie.entity.Category;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CategoryMapper {

    @Select("select * from category where  name = #{name};")
    Category findByName(String name);

    @Insert("insert into category values (null,#{type},#{name},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser});")
    Integer insert(Category category);

    @Select("select * from category order by sort")
    List<Category> selectAllOrderBySort();

    @Delete("delete from category where id = #{id}")
    Integer deleteById(Long id);

    /**
     * 判断这个类别名称是否存在(排除自己)
     * @param category
     * @return
     */
    @Select("select count(*) from category where name = #{name} and id != #{id}")
    Integer findByName2(Category category);

    /**
     * 修改分类
     * @param category
     * @return
     */
    Integer update(Category category);

    /**
     * 查询所有分类
     * @return
     */
    @Select("select * from category;")
    List<Category> findAll();

    /**
     * 通过类型查询所有分类数据
     * @param type
     * @return
     */
    List<Category> findAllByType(Integer type);

    /**
     * 通过id查找分类
     * @param id
     * @return
     */
    @Select("select * from category where id = #{id} ")
    Category findById(Long id);
}
